package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.article.ArticleListener;
import com.prp.gwt.client.vo.SubjectChildCategoryList;

public class CreateEntityPanel extends VerticalPanel {

	private static CreateEntityPanel instance;
	private HTML commit = new HTML("<a>下一步</a>");
	private FlexTable radioBox = new FlexTable();
	private RadioButton[] rbs;
	private TextBox input = new TextBox();
	private HorizontalPanel titlePanel = new HorizontalPanel();
	private Label label = new Label();
	private SubjectChildCategoryList list;
	
	public static CreateEntityPanel getInstance() {
		if (instance == null)
			instance = new CreateEntityPanel();
		return instance;
	}

	private CreateEntityPanel() {
		addStyleName("createEntity");
		titlePanel.addStyleName("createEntity-title");
		titlePanel.add(label);
		label.addStyleName("createEntity-title-text");
		add(titlePanel);
		VerticalPanel innerPanel = new VerticalPanel();
		innerPanel.addStyleName("createEntity-content");
		HorizontalPanel namePanel = new HorizontalPanel();
		Label l1 = new Label("条目名称:");
		l1.addStyleName("createEntity-label");
		namePanel.add(l1);
		namePanel.add(input);
		namePanel.addStyleName("createEntity-item");
		innerPanel.add(namePanel);
		HorizontalPanel categoryPanel = new HorizontalPanel();
		Label l2 = new Label("类别:");
		l2.addStyleName("createEntity-label");
		categoryPanel.add(l2);
		categoryPanel.add(radioBox);
		innerPanel.add(categoryPanel);
		HorizontalPanel p = new HorizontalPanel();
		p.add(commit);
		innerPanel.add(p);
		commit.setStylePrimaryName("createEntity-commit");
		add(innerPanel);
		
		commit.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(MouseOverEvent event) {
				commit.addStyleDependentName("over");
			}
		});
		commit.addMouseOutHandler(new MouseOutHandler(){
			@Override
			public void onMouseOut(MouseOutEvent event) {
				commit.removeStyleDependentName("over");
			}
		});
		commit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(input.getText().isEmpty()){
					Window.alert("请输入条目名称");
					return;
				}
				for (int i = 0; i < rbs.length; i++)
					if (rbs[i].getValue()) {
						ArticleListener.getInstance().createEntity(
								list.get(i).getId(), input.getText());
						return;
					}
			}
		});
	}

	public void load(final SubjectChildCategoryList list) {
		this.list = list;
		label.setText("为主题"+list.getParent()+"添加新条目");
		radioBox.clear();
		input.setText("");
		rbs = new RadioButton[list.size()];
		for (int i = 0; i < list.size(); i++) {
			rbs[i] = new RadioButton("childCategory", list.get(i).getName());
			radioBox.setWidget(i/4, i%4, rbs[i]);
		}
		rbs[0].setValue(true);
	}
	
}
